package com.my.aslife.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.my.aslife.mapper.FeedBackMapper;
import com.my.aslife.module.common.PageRequestVo;
import com.my.aslife.module.common.RedisConstant;
import com.my.aslife.module.common.ResponseResult;
import com.my.aslife.module.common.UserIdThreadLocal;
import com.my.aslife.module.entity.Consult;
import com.my.aslife.module.entity.Feedback;
import com.my.aslife.service.FeedBackService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author mayuan
 * @description TODO
 */
@Service
@Slf4j
public class FeedBackServiceImpl extends ServiceImpl<FeedBackMapper, Feedback> implements FeedBackService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private FeedBackMapper feedBackMapper;

    @Override
    public ResponseResult commitFeed(Feedback feedback) {

        feedback.setCreateTime(new Date());
        save(feedback);

        return ResponseResult.ok();
    }

    @Override
    public ResponseResult getFeedBackList(Long uid) {

        String feedBackKey = RedisConstant.USER_FEEDBACK_NULL + uid;
        String FeedBackFlag = stringRedisTemplate.opsForValue().get(feedBackKey);
        if (!StringUtils.isEmpty(FeedBackFlag)) {
            return ResponseResult.ok();
        }

        LambdaQueryWrapper<Feedback> lqw = new LambdaQueryWrapper<>();
        lqw.orderByDesc(Feedback::getStatus);
        lqw.orderByDesc(Feedback::getCreateTime);
        List<Feedback> list = list(lqw);

        if (list == null || list.size() == 0) {
            stringRedisTemplate.opsForValue().set(feedBackKey, "null",
                    RedisConstant.ONE_HOUR, TimeUnit.HOURS);
        }

        return ResponseResult.ok(list);
    }

    @Override
    public ResponseResult selectFeedBackInfoByPageAndCondition(PageRequestVo pageRequestVo) {

        Page<Feedback> page = new Page<>(pageRequestVo.getCurrent(), pageRequestVo.getPageSize());
        Page<Feedback> page1 = page(page);
        List<Feedback> records = page1.getRecords();

        return ResponseResult.ok(records, count());
    }

    @Override
    public ResponseResult queryFeedBackInfoByFeedBackId(Long id) {
        return ResponseResult.ok(getById(id));
    }

    @Override
    public ResponseResult commitAdminFeedBack(Feedback feedback) {
        LambdaQueryWrapper<Feedback> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Feedback::getId, feedback.getId());
        String s = feedback.getProcessComment();
        Feedback one = getOne(lqw);
        BeanUtils.copyProperties(one, feedback);
        feedback.setStatus((short) 1);
        feedback.setProcessTime(new Date());
        feedback.setProcessComment(s);
        feedback.setProcessId(Long.valueOf(UserIdThreadLocal.get()));
        saveOrUpdate(feedback);
        return ResponseResult.ok();
    }
}
